package com.ruoyi.web.controller.vantController.customerController;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.customer.PasswordTeamCode;
import com.ruoyi.system.service.vantservice.customerervice.PasswordTeamCodeService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/password")
public class ApiPasswordTeamCodeController extends BaseController {

    @Autowired
    private PasswordTeamCodeService passwordTeamCodeService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private HttpServletRequest request;
    /**
     * 查询施工队支付密码列表
     */

    @GetMapping("/code/list")
    public List<PasswordTeamCode> list(PasswordTeamCode passwordTeamCode)
    {

        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        passwordTeamCode.setCustomerId(customerLogin.getCustomerId());
        List<PasswordTeamCode> list = passwordTeamCodeService.selectPasswordTeamCodeList(passwordTeamCode);
        return list;
    }
//    public TableDataInfo list(PasswordTeamCode passwordTeamCode)
//    {
//        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
//        passwordTeamCode.setCustomerId(customerLogin.getCustomerId());
//        List<PasswordTeamCode> list = passwordTeamCodeService.selectPasswordTeamCodeList(passwordTeamCode);
//        return getDataTable(list);
//    }




    /**
     * 获取施工队支付密码详细信息
     */
    @GetMapping(value = "/code/{passwordId}")
    public AjaxResult getInfo(@PathVariable("passwordId") Long passwordId)
    {
        return AjaxResult.success(passwordTeamCodeService.selectPasswordTeamCodeById(passwordId));
    }

    /**
     * 新增施工队支付密码
     */

    @PostMapping(value = "/code/add")
    public AjaxResult add(@RequestBody PasswordTeamCode passwordTeamCode)
    {
        if (UserConstants.NOT_UNIQUE.equals(passwordTeamCodeService.checkUserNameUnique(passwordTeamCode)))
        {
            return AjaxResult.error("已设置密码");
        }

        return toAjax(passwordTeamCodeService.insertPasswordTeamCode(passwordTeamCode));
    }

    /**
     * 修改施工队支付密码
     */

    @PutMapping(value = "/code/edit")
    public AjaxResult edit(@RequestBody PasswordTeamCode passwordTeamCode)
    {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        passwordTeamCode.setCustomerId(customerLogin.getCustomerId());

//        Long numcustomerId = passwordTeamCode.getNumcustomerId();
//        passwordTeamCode.setCustomerId(numcustomerId);

        String passwordnum = passwordTeamCode.getPasswordnum();
        System.err.println("****passwordnum*****"+passwordnum);
        passwordTeamCode.setPasswordcode(passwordnum);
        return toAjax(passwordTeamCodeService.updatePasswordTeamCode(passwordTeamCode));
    }

    /**
     * 删除施工队支付密码
     */

    @DeleteMapping("/code/{passwordIds}")
    public AjaxResult remove(@PathVariable Long[] passwordIds)
    {
        return toAjax(passwordTeamCodeService.deletePasswordTeamCodeByIds(passwordIds));
    }

}
